######################################
###   makefile                     ###
###                                ###
###   Organización de Datos        ###
###   2do. cuatrimestre 2012       ###
###                                ###
###   TP - Entrega 1               ###
###                                ###
###   Grupo:                       ###
###                                ###
###   Orquera, Federico  (84333)   ###
###   Famiglietti, Bruno (85997)   ###
###   Torchio, Mariano   (85998)   ###
###   Ripetour, Diego    (86601)   ###
###                                ###
######################################

# Instrucciones para agregar una clase en alguna libreria:							\
 Van al anexo de "Registro de Sources" y agregan los nombres de los .cpp que 		\
 tengan las clases que quieren agregar. Hay que agregarlos usando el path relativo  \
 al directorio de la capa ( miren los que ya hay cargados )

# Instrucciones para agregar un test ejecutable:																\
 En la parte de ejecutables: 																					\
 NOMBRE_DEL_TEST_ts: $(LOGIC_DIR)DIRECTORIO_DEL_MODULO/SOURCE_CON_MAIN_DEL_TEST.cpp liblogic.a libphysical.a	\
 	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical						\ 																												\
  Después para usarlo hacen desde el directorio raiz: make NOMBRE_DEL_TEST_ts

# Instrucciones para agregar el test al target "tests" 								\
	Van a donde dice "tests: blah blah blah" y         								\
	al final agregan el nombre del target que crearon (NOMBRE_DEL_TEST_ts) 			\
	finalmente llaman a "make tests" para compilar todo.							\
	NOTA: POR FAVOR SI HACEN ESTO REVISEN Q TODO COMPILE ANTES DE SUBIR ALGO AL SVN
	


# Variables de Configuración
SHELL = /bin/sh
VPATH = $(OBJ_DIR) $(BIN_DIR) $(TEST_DIR)
CXX = g++
ARCHIVER = ar
AFLAGS = -r
CXXARGS = -O0 -g3 -Wall -fmessage-length=0 -std=c++0x
CXXFLAGS = -O0 -g3 -c -MMD -MP 
LDFLAGS = -I$(PHYSICAL_DIR) -I$(LOGIC_DIR)
RM = rm
RMFLAGS = -f -r
MKDIR = mkdir
MKDIRFLAGS = -p
CP = cp

# Directorios

RUNTEST_DIR = extras/runtest/
TOOLS_DIR	= extras/tools/
TESTFILES_DIR = extras/testfiles/
OBJ_DIR = obj/
BIN_DIR = bin/
TEST_DIR = test/
SRC_DIR = src/
PHYSICAL_DIR = $(SRC_DIR)physical_layer/
LOGIC_DIR  = $(SRC_DIR)logic_layer/
APPLICATION_DIR = $(SRC_DIR)application_layer/
SOURCES_FP = 

# Registro de Sources

# Sources de la capa Física:
PHYSICAL_S = FixedSizeRegisterSequentialFile.cpp \
			 FixedSizeBlockFile.cpp \
			 FreeBlocksFile.cpp \
			 VariableStringSerializer.cpp \
			 # fin de lista de sources de la capa física
			 
# Sources de la capa Lógica:
LOGIC_S = external_sort/DictionaryTermSerializer.cpp \
          external_sort/DictionaryTerm.cpp \
          external_sort/DictionaryTermsFile.cpp \
          external_sort/ExternalSort.cpp \
          external_sort/FileManager.cpp \
          external_sort/FileMerger.cpp \
          hash/HashRecord.cpp \
          hash/HashRecordSerializer.cpp \
          hash/HashBucket.cpp \
          hash/HashBucketSerializer.cpp \
          hash/HashFile.cpp \
          hash/archivoDirecto.cpp \
          bplustree/bplusfilehandler/LeafRegisterSerializer.cpp \
          bplustree/bplusfilehandler/FrontCodingEncoder.cpp \
          bplustree/bplusfilehandler/LeafNodeSerializer.cpp \
          bplustree/bplusfilehandler/InternalRegisterSerializer.cpp \
          bplustree/bplusfilehandler/InternalNodeSerializer.cpp \
          bplustree/bplusfilehandler/BPlusTreeFile.cpp \
          bplustree/bplusfilehandler/BPlusTreeDefinitions.cpp \
          # fin de lista de sources de la capa logica

# Sources de la capa de Aplicación:
APPLICATION_S = 

# Registro de template classes (por módulo)
PHYSICAL_TEMPLATES = $(PHYSICAL_DIR)ListSerializer.h \
					 $(PHYSICAL_DIR)VariableSizeRecordSerializer.h \
					 # fin de physical templates

LOGIC_TEMPLATES = $(LOGIC_DIR)external_sort/tools/Heap.h \
				  $(LOGIC_DIR)bplustree/bplusregistertemplates/BPlusSimpleRegisterTemplate.h \
  				  $(LOGIC_DIR)bplustree/bplusregistertemplates/BPlusSimpleRegisterList.h \
  				  $(LOGIC_DIR)bplustree/bplusnodes/NewNode.h \
  				  $(LOGIC_DIR)bplustree/bplusnodes/NewLeaf.h \
  				  $(LOGIC_DIR)bplustree/bplusnodes/NewInternal.h \
  				  $(LOGIC_DIR)bplustree/bplusfilehandler/NewHandlerInterface.h \
  				  $(LOGIC_DIR)bplustree/BPlusTreeTemplate.h
  				  # fin de logic templates
  				  
APPLICATION_TEMPLATES = 

TEMPLATES_FP = $(PHYSICAL_TEMPLATES) $(LOGIC_TEMPLATES) $(APPLICATION_TEMPLATES)

# Lógica de parseo de dependencias y demás
PHYSICAL_S_FP = $(foreach source, $(PHYSICAL_S), $(addprefix $(PHYSICAL_DIR), $(source) ) )
PHYSICAL_O = $(patsubst %.cpp, %.o, $(notdir $(PHYSICAL_S_FP)) )
override SOURCES_FP += $(PHYSICAL_S_FP)

LOGIC_S_FP = $(foreach source, $(LOGIC_S), $(addprefix $(LOGIC_DIR), $(source) ) )
LOGIC_O = $(patsubst %.cpp, %.o, $(notdir $(LOGIC_S_FP)) )
override SOURCES_FP += $(LOGIC_S_FP)

APPLICATION_S_FP = $(foreach source, $(APPLICATION_S), $(addprefix $(APPLICATION_DIR), $(source) ) )
APPLICATION_O = $(patsubst %.cpp, %.o, $(notdir $(APPLICATION_S_FP)) )
override SOURCES_FP += $(APPLICATION_S_FP)

# Headers (utilizados como dependencias para el rebuild)
# Capa Física
PHYSICAL_H = $(patsubst %.cpp, %.h, $(PHYSICAL_S_FP) )
# Capa Lógica
LOGIC_H = $(patsubst %.cpp, %.h, $(LOGIC_S_FP) )
# Capade Aplicacion
APPLICATION_H = $(patsubst %.cpp, %.h, $(APPLICATION_S_FP) )

HEADERS_FP = $(PHYSICAL_H)
override HEADERS_FP += $(LOGIC_H)
override HEADERS_FP += $(APPLICATION_H)

override VPATH += $(dir $(SOURCES_FP))
override VPATH += $(dir $(TEMPLATES_FP))

# Reglas del Makefile

.PHONY: all clean createDir libs executables tests tools
	
# MODIFICAR ESTA REGLA!!!
all: clean tests executables #createDirs libs tests
	
createDirs:
	$(MKDIR) $(MKDIRFLAGS) $(OBJ_DIR) $(BIN_DIR) $(TEST_DIR)
	
clean: 
	$(RM) $(RMFLAGS) $(BIN_DIR)* $(OBJ_DIR)* $(TEST_DIR)*
	
libs: libphysical.a liblogic.a
	
executables: sortApp treeApp hashApp statsApp
	
tests: createDirs libs tools runtest fsrsf_ts \
		dtf_ts fsbf_ts fbf_ts vss_ts ls_ts    \
		hrs_ts hbs_ts hb_ts fc_ts lrs_ts      \
	    lns_ts irs_ts ins_ts bptf_ts bpt_ts   \
	    treeApp_ts
	$(CP) $(TESTFILES_DIR)* $(TEST_DIR)

tools: hash2text text2hash lrs_formater

# Ejecutables
sortApp: $(SRC_DIR)main.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -llogic -lphysical
	
hashApp: $(APPLICATION_DIR)main_Hash.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -llogic -lphysical
	
treeApp: $(APPLICATION_DIR)main_Tree.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -llogic -lphysical
	
statsApp: $(APPLICATION_DIR)main_stats.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -llogic -lphysical

# Tests Suites
runtest: $(RUNTEST_DIR)runtest.cc
	$(CXX) $(CXXARGS) -o "$(TEST_DIR)runtest" "$<"

treeApp_ts: $(APPLICATION_DIR)main_Tree.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical

lrs_formater: $(TOOLS_DIR)lrs_formater.cpp
	$(CXX) $(CXXARGS) -o "$(TEST_DIR)$@" "$<"
	
fsrsf_ts: $(PHYSICAL_DIR)FixedSizeRegisterSequentialFile_test.cpp libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -lphysical
	
fsbf_ts: $(PHYSICAL_DIR)FixedSizeBlockFile_test.cpp libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -lphysical

fbf_ts: $(PHYSICAL_DIR)FreeBlocksFile_test.cpp libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -lphysical
	
vss_ts: $(PHYSICAL_DIR)VariableStringSerializer_test.cpp libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -lphysical
	
ls_ts: $(PHYSICAL_DIR)ListSerializer_test.cpp libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -lphysical
	
hrs_ts: $(LOGIC_DIR)hash/HashRecordSerializer_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
hbs_ts: $(LOGIC_DIR)hash/HashBucketSerializer_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
hb_ts: $(LOGIC_DIR)hash/HashBucket_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
text2hash: $(LOGIC_DIR)hash/text2hash.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
hash2text: $(LOGIC_DIR)hash/hash2text.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
dtf_ts: $(LOGIC_DIR)external_sort/DictionaryTermsFile_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical

hash_ts: $(LOGIC_DIR)hash/hashTests.cpp liblogic.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic

sort_ts: $(LOGIC_DIR)external_sort/ExternalSort_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
fc_ts: $(LOGIC_DIR)bplustree/bplusfilehandler/FrontCodingEncoder_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
lrs_ts: $(LOGIC_DIR)bplustree/bplusfilehandler/LeafRegisterSerializer_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
lns_ts: $(LOGIC_DIR)bplustree/bplusfilehandler/LeafNodeSerializer_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical

irs_ts: $(LOGIC_DIR)bplustree/bplusfilehandler/InternalRegisterSerializer_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical

ins_ts: $(LOGIC_DIR)bplustree/bplusfilehandler/InternalNodeSerializer_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical

bptf_ts: $(LOGIC_DIR)bplustree/bplusfilehandler/BPlusTreeFile_test.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical

bpt_ts: $(LOGIC_DIR)bplustree/test_tree_with_file.cpp liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(TEST_DIR)$@" "$<" -llogic -lphysical
	
# Librerias
libphysical.a: $(PHYSICAL_O) $(PHYSICAL_TEMPLATES) $(PHYSICAL_H)
	$(ARCHIVER) $(AFLAGS) "$(OBJ_DIR)libphysical.a" $(addprefix $(OBJ_DIR), $(PHYSICAL_O))
	
liblogic.a: $(LOGIC_O) $(LOGIC_TEMPLATES) $(LOGIC_H)
	$(ARCHIVER) $(AFLAGS) "$(OBJ_DIR)liblogic.a" $(addprefix $(OBJ_DIR), $(LOGIC_O))
	
libapplication.a: $(APPLICATION_O) $(APPLICATION_TEMPLATES) $(APPLICATION_H)
	$(ARCHIVER) $(AFLAGS) "$(OBJ_DIR)libapplication.a" $(addprefix $(OBJ_DIR), $(APPLICATION_O))
	

# Objetos
.SECONDEXPANSION:
%.o : %.cpp %.h #$$(filter %$$*.cpp, $(SOURCES_FP))
	$(CXX) $(CXXFLAGS) $(CXXARGS) -MF"$(OBJ_DIR)$*.d" -MT"$(OBJ_DIR)$*.d" -o"$(OBJ_DIR)$@" "$(filter %$*.cpp, $(SOURCES_FP))"
	
